<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="hid">
      <el-input v-model="dataForm.hid" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="userid">
      <el-input v-model="dataForm.userid" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="state">
      <el-input v-model="dataForm.state" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="type">
      <el-input v-model="dataForm.type" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="created">
      <el-input v-model="dataForm.created" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="attachfile">
      <el-input v-model="dataForm.attachfile" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="stateid">
      <el-input v-model="dataForm.stateid" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="pid">
      <el-input v-model="dataForm.pid" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="cid">
      <el-input v-model="dataForm.cid" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="aid">
      <el-input v-model="dataForm.aid" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="message">
      <el-input v-model="dataForm.message" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="username">
      <el-input v-model="dataForm.username" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="idcard">
      <el-input v-model="dataForm.idcard" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="sex">
      <el-input v-model="dataForm.sex" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="tel">
      <el-input v-model="dataForm.tel" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="indate">
      <el-input v-model="dataForm.indate" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="outdate">
      <el-input v-model="dataForm.outdate" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          hid: '',
          userid: '',
          state: '',
          type: '',
          created: '',
          attachfile: '',
          stateid: '',
          pid: '',
          cid: '',
          aid: '',
          message: '',
          username: '',
          idcard: '',
          sex: '',
          tel: '',
          indate: '',
          outdate: ''
        },
        dataRule: {
          hid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          userid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          state: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          created: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          attachfile: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          stateid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          pid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          cid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          aid: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          message: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          username: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          idcard: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          tel: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          indate: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          outdate: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/apply/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.hid = data.apply.hid
                this.dataForm.userid = data.apply.userid
                this.dataForm.state = data.apply.state
                this.dataForm.type = data.apply.type
                this.dataForm.created = data.apply.created
                this.dataForm.attachfile = data.apply.attachfile
                this.dataForm.stateid = data.apply.stateid
                this.dataForm.pid = data.apply.pid
                this.dataForm.cid = data.apply.cid
                this.dataForm.aid = data.apply.aid
                this.dataForm.message = data.apply.message
                this.dataForm.username = data.apply.username
                this.dataForm.idcard = data.apply.idcard
                this.dataForm.sex = data.apply.sex
                this.dataForm.tel = data.apply.tel
                this.dataForm.indate = data.apply.indate
                this.dataForm.outdate = data.apply.outdate
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/hotel/apply/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'hid': this.dataForm.hid,
                'userid': this.dataForm.userid,
                'state': this.dataForm.state,
                'type': this.dataForm.type,
                'created': this.dataForm.created,
                'attachfile': this.dataForm.attachfile,
                'stateid': this.dataForm.stateid,
                'pid': this.dataForm.pid,
                'cid': this.dataForm.cid,
                'aid': this.dataForm.aid,
                'message': this.dataForm.message,
                'username': this.dataForm.username,
                'idcard': this.dataForm.idcard,
                'sex': this.dataForm.sex,
                'tel': this.dataForm.tel,
                'indate': this.dataForm.indate,
                'outdate': this.dataForm.outdate
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
